package cn.kmia.hrm.service.basicData;

import cn.kmia.hrm.pojo.PostLinkInfo;
import cn.kmia.hrm.service.BaseService;
import cn.kmia.hrm.util.CommonSet;
import org.nutz.dao.Chain;
import org.nutz.dao.Cnd;
import org.nutz.dao.QueryResult;
import org.nutz.dao.Sqls;
import org.nutz.dao.pager.Pager;
import org.nutz.dao.sql.Sql;
import org.nutz.dao.sql.SqlCallback;
import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.lang.Strings;
import org.nutz.lang.util.NutMap;
import org.nutz.log.Log;
import org.nutz.log.Logs;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.*;

/**
 * @program: hrm
 * @description: 岗位关联信息服务
 * @author: 4K
 * @create: 2018-07-19 16:20
 **/
@IocBean
public class PostLinkInfoService extends BaseService {

    private static final Log log = Logs.get();

    /**
     * 分页查询
     *
     * @param name
     * @param seq
     * @param pager
     * @return
     */
    public QueryResult queryByPage(String name, String seq, Pager pager) {
        Cnd cnd = Cnd.where("1", "=", 1);
        if (!Strings.isBlank(name) && !"null".equals(name)) {
            cnd.and("name", "like", "%" + name + "%");
        }
        if (!Strings.isBlank(seq) && !"null".equals(seq)) {
            cnd.and("post_seq", "=", seq);
        }
        cnd.and("delFlag", "is", null);
        cnd.orderBy("post_seq","esc");
        List<PostLinkInfo> list = dao.query(PostLinkInfo.class, cnd, pager);
        QueryResult qr = new QueryResult();
        qr.setList(list);
        pager.setRecordCount(dao.count(PostLinkInfo.class, cnd));
        qr.setPager(pager);
        return qr;
    }

    /**
     * 查询
     *
     * @param seq
     * @return
     */
    public List<PostLinkInfo> query(String seq) {
        return dao.query(PostLinkInfo.class, Cnd.where("delFlag", "is", null).and("post_seq", "=", seq).orderBy("post_seq","esc"));
    }

    /**
     * 新增
     *
     * @param postLinkInfo    对象信息
     * @param user_idcard 操作人
     * @return
     */
    public PostLinkInfo add(PostLinkInfo postLinkInfo, String user_idcard) {

        postLinkInfo.setOpBy(user_idcard);
        postLinkInfo.setCreateTime(new Timestamp(new Date().getTime()));
        postLinkInfo.setUpdateTime(new Timestamp(new Date().getTime()));
        postLinkInfo = dao.insert(postLinkInfo);
        updatePostLinkInfo();
        return postLinkInfo;
    }

    /**
     * 更新
     *
     * @param postLinkInfo    对象信息
     * @param user_idcard 操作人
     * @return
     */
    public boolean update(PostLinkInfo postLinkInfo, String user_idcard) {
        postLinkInfo.setOpBy(user_idcard);
        postLinkInfo.setUpdateTime(new Timestamp(new Date().getTime()));
        if (dao.updateIgnoreNull(postLinkInfo) > 0){
            updatePostLinkInfo();
            return true;
        }else
            return false;
    }

    /**
     * 删除，实际标记delFlag为true
     *
     * @param ids
     * @return
     */
    public NutMap del(int[] ids) {
        NutMap nm = new NutMap();
        if (ids != null && ids.length > 0) {
            dao.update(PostLinkInfo.class, Chain.make("delFlag", true), Cnd.where("id", "in", ids));
            updatePostLinkInfo();
            nm.setv("success", true);
        } else {
            nm.setv("success", false).setv("errorMsg", "请重试！");
        }
        return nm;
    }

    /**
     * 获取对象
     *
     * @param id
     * @return
     */
    public PostLinkInfo getPostLinkInfo(int id) {
        return dao.fetch(PostLinkInfo.class, Cnd.where("id", "=", id));
    }

    /**
     * 查询岗位序列列表
     * @return
     */
    public List<String> querySeq() {
        Sql sql = Sqls.create("select post_seq from post_link_info $condition");
        sql.setCondition(Cnd.where("delFlag", "is", null).orderBy("post_seq","esc"));
        sql.setCallback(new SqlCallback() {
            public Object invoke(Connection conn, ResultSet rs, Sql sql) throws SQLException {
                List<String> seqs = new ArrayList<>();
                while (rs.next())
                    seqs.add(rs.getString("post_seq"));
                return seqs;
            }
        });
        dao.execute(sql);
        return sql.getList(String.class);
    }

    /**
     * 查询岗位名称列表
     * @param seq
     * @return
     */
    public List<String> queryPostNames(String seq) {
        String sqlStr = "select post_name from post_link_info";
        if(!Strings.isBlank(seq)) {
            sqlStr += " $condition";
        }
        Sql sql = Sqls.create(sqlStr);
        if(!Strings.isBlank(seq)) {
            sql.setCondition(Cnd.where("post_seq", "=", seq).and("delFlag", "is", null).orderBy("post_seq","esc"));
        }
        sql.setCallback(new SqlCallback() {
            public Object invoke(Connection conn, ResultSet rs, Sql sql) throws SQLException {
                List<String> names = new ArrayList<>();
                while (rs.next())
                    names.add(rs.getString("post_name"));
                return names;
            }
        });
        dao.execute(sql);
        return sql.getList(String.class);
    }

    /**
     * 更新岗位关联信息
     */
    public void updatePostLinkInfo(){
        CommonSet.PostLinkInfo = new HashMap<>();
        List<String> seqs = this.querySeq();
        List<String> postNames;
        for(String seq : seqs){
            postNames = this.queryPostNames(seq);
            CommonSet.PostLinkInfo.put(seq,postNames);
        }
    }

    /**
     * 查询岗位名称对应资质
     * @return
     */
    public void updatePostNameRequire() {
        Sql sql = Sqls.create("select post_name,post_require from post_link_info $condition");
        sql.setCondition(Cnd.where("delFlag", "is", null));
        sql.setCallback(new SqlCallback() {
            public Object invoke(Connection conn, ResultSet rs, Sql sql) throws SQLException {
                Map<String,String> map = new HashMap<>();
                while (rs.next())
                    map.put(rs.getString("post_name"),rs.getString("post_require"));
                return map;
            }
        });
        dao.execute(sql);
        CommonSet.PostNameRequireInfo = (Map<String, String>) sql.getResult();
    }
}
